from datetime import datetime
from app import db


# 排行榜产品链接表
class AmzRanking(db.Model):
    __tablename__ = 'amz_ranking'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    url = db.Column(db.String(512), nullable=False)
    status = db.Column(db.Integer, default=0)  # 0: 未执行, 1: 已执行
    type = db.Column(db.String(50), nullable=False)

    # 定义与 amz_content 表的关系
    contents = db.relationship('AmzContent', backref='amz_ranking', lazy=True)


# 产品详情表
class AmzContent(db.Model):
    __tablename__ = 'amz_content'
    id = db.Column(db.Integer, primary_key=True)
    ASIN = db.Column(db.String(20), nullable=False)
    brand_name = db.Column(db.String(255), nullable=False)
    ranking = db.Column(db.String(255), nullable=False)
    fba_json = db.Column(db.JSON, nullable=True)
    create_time = db.Column(db.DateTime, default=datetime.utcnow)
    type = db.Column(db.String(50), nullable=False)
    rank_id = db.Column(db.Integer, db.ForeignKey('amz_ranking.id'), nullable=False)


# 产品监控表
class AmzSupervisoryControl(db.Model):
    __tablename__ = 'amz_supervisory_control'
    id = db.Column(db.Integer, primary_key=True)
    ASIN = db.Column(db.String(20), nullable=False)
    brand_name = db.Column(db.String(255), nullable=False)
    ranking = db.Column(db.String(255), nullable=False)
    fba_json = db.Column(db.JSON, nullable=True)
    type = db.Column(db.String(50), nullable=False)
    create_time = db.Column(db.DateTime, default=datetime.utcnow)

class RankingTable(db.Model):
    __tablename__ = 'ranking_table'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    url = db.Column(db.Text, nullable=False)
    pate_id = db.Column(db.Integer, nullable=False)
    status = db.Column(db.Integer, default=0)


class RankingTwoTable(db.Model):
    __tablename__ = 'ranking_two_table'
    id = db.Column(db.Integer, primary_key=True)
    url = db.Column(db.String(255), nullable=False)
    ranking_id = db.Column(db.Integer, nullable=False)
